package cn.iocoder.yudao.module.design.asyncworker.flowTask;


import cn.iocoder.yudao.module.design.controller.admin.flowtask.vo.FlowTaskCreateReqVO;
import cn.iocoder.yudao.module.design.dal.dataobject.problem.ProblemDO;
import cn.iocoder.yudao.module.design.service.flowtask.FlowTaskService;
import cn.iocoder.yudao.module.design.service.problem.ProblemService;
import com.tlcsdm.async.callback.ICallback;
import com.tlcsdm.async.callback.IWorker;
import com.tlcsdm.async.executor.timer.SystemClock;
import com.tlcsdm.async.worker.WorkResult;
import com.tlcsdm.async.wrapper.WorkerWrapper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 关联问题全部完成任务开启通知
 * 使用方法见：https://gitee.com/jd-platform-opensource/asyncTool/blob/master/QuickStart.md
 * @author Annie
 * @ClassName FlowTaskNotifyWorker
 * @date 2023年07月29日
 * @version: 1.0
 */
@Slf4j
public class ProblemRelEndTaskNotifyWorker implements IWorker<String, String>, ICallback<String, String> {
    @Resource
    private ProblemService problemService;
    @Resource
    private FlowTaskService flowTaskService;
    
    @Override
    public void begin() {
        System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
    }

    /**
     * @param success
     * @param param
     * @param workResult
     */
    @Override
    public void result(boolean success, String param, WorkResult<String> workResult) {
        if (success) {
            System.out.println("callback FlowTaskNotifyWorker success--" + SystemClock.now() + "----" + workResult.getResult()
                    + "-threadName:" +Thread.currentThread().getName());
        } else {
            System.err.println("callback FlowTaskNotifyWorker failure--" + SystemClock.now() + "----"  + workResult.getResult()
                    + "-threadName:" +Thread.currentThread().getName());
        }
    }

    @Override
    public String action(String problemId, Map<String, WorkerWrapper> map) {
        FlowTaskCreateReqVO data =null ;
        List<ProblemDO> relProblemList = problemService.getRelUnProblemList(Long.valueOf(problemId) );
        for (ProblemDO problemDO:relProblemList    ) {

            // 将挂起的问题 修改问待解决
            problemService.updateProblemStatus(problemDO.getId(),"1");

            data = new FlowTaskCreateReqVO();
            String title = "【"+problemDO.getTitle()+"】 问题状态变更";
            data.setTaskName(title);
            // 通知类型
            data.setTaskType("3");
            data.setHouseId(problemDO.getHouseId());
            data.setFlowId(problemDO.getFlowId());
            data.setNodeId(problemDO.getNodeId());
            data.setDealerId(problemDO.getUserId());
            data.setRemark("系统提醒：\n"+title+"   状态已由挂起 改为  待解决 请关注");
            // 通知
            data.setItemType("0");
            // 任务等级 正常
            data.setTaskLevel("1");
            // 给创建人发通知
            flowTaskService.createFlowTask(data);
            log.debug("创建人发通知 已完成  param = %s" ,data);
            // 给处理人发通知
            data.setDealerId(problemDO.getDealerId());
            flowTaskService.createFlowTask(data);
            log.debug("处理人发通知 已完成 param = %s" ,data);
            log.debug("项目： %s  标题：%s ,处理人： %s " ,data.getProjectName(),data.getTaskName(),data.getDealerId());


        }
        return "FlowTaskNotifyWorker = " + SystemClock.now() + "---problemId= " + problemId + " from 1";
    }

    /**
     * @return
     */
    @Override
    public String defaultValue() {
        return "FlowTaskNotifyWorker--default";
    }
}
